package communication;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import lejos.nxt.*;
import lejos.nxt.comm.*;
import lejos.util.TextMenu;

/**
 * @version   1.0

 * @author    Bruno Argenta, Esteban Arrua, Luis Galusso
 * @copyright (C) 2012 Bruno Argenta, Esteban Arrua, Luis Galusso
 * @contact   b.argenta@gmail.com, estarr2212@gmail.com, lgalusso@gmail.com
 *
 * @license        GNU/GPLv3
 * This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see http://www.gnu.org/licenses/.
 *
 *
*/

/**
 * Create an LCP responder to handle LCP requests. Allow the
 * User to choose between Bluetooth, USB and RS485 protocols.
 * 
 * @author Andy Shaw
 *
 */
public class NXTLCPRespond
{
    /**
     * Our local Responder class so that we can over-ride the standard
     * behaviour. We modify the disconnect action so that the thread will
     * exit.
     */
    static class Responder extends LCPResponder
    {
        Responder(NXTCommConnector con)
        {
            super(con);
        }

        protected void disconnect()
        {
            super.disconnect();
            super.shutdown();
        }
    }

    public static void main(String[] args) throws Exception
    {
       // String[] connectionStrings = new String[]{"Bluetooth", "USB", "RS485"};
        //TextMenu connectionMenu = new TextMenu(connectionStrings, 1, "Connection");
        //NXTCommConnector[] connectors = {Bluetooth.getConnector(), USB.getConnector(), RS485.getConnector()};

        //int connectionType = connectionMenu.select();
        
        LCD.clear();
        LCD.clear();
        //LCD.drawString("Type: " + connectionStrings[connectionType], 0, 0);
        //Button.waitForAnyPress();
        LCD.drawString("Running...", 0, 1);
        NXTRegulatedMotor barredora = new NXTRegulatedMotor(MotorPort.A);
        //LCD.drawString("Running...", 0, 1);
       
        LCD.drawString("despues de la conexion", 0, 1);
       
        LCD.drawString("despues de open data", 0, 1);
        int n;
        barredora.setSpeed(758);
        boolean conexion = false;
        while (true){
        	 NXTConnection resp = RS485.getConnector().waitForConnection(0, NXTConnection.RAW);
        	 DataInputStream dis = resp.openDataInputStream();
        	 DataOutputStream dos = resp.openDataOutputStream();
        	 conexion = true;
        	 while (conexion) {
        		n = dis.readInt();
             	dos.writeInt(1);
             	dos.flush();
             	if (n==1){
             		barredora.forward();
             	}else{
             		barredora.stop();
             		conexion = false;
             	}
        	 }
        	dis.close();
        	dos.close();
        	resp.close();
        	//LCD.drawString("barredora "+Integer.toString(n), 0, 1);
        	  	
        }        
    }
}